'''
Created on 20/02/2012

@author: Bruna
'''
from serial_controller import SerialController
from view_controller import ViewController

class Controller(object):
    '''
    Main Controller
    '''

    def __init__(self):
        '''
        Constructor
        '''
        self.connected = False
        self.serial_controller = SerialController(self)
        self.view_controller = ViewController(self)
        self.start()
        
    def start(self):
        '''
        Start application
        '''
        self.view_controller.show()
        
        
    def is_connected(self):
        '''
        Verifies connection
        
        @return: if a connection was established
        '''
        return self.connected
    
    def connection(self):
        '''
        Calls local  __connect/__disconnect method depending on connection state
        '''
        if self.is_connected():
            try:
                self.__disconnect()
            except Exception as e:
                self.view_controller.print_error("Connection could not be closed.")
                print e
        else:
            try:
                self.__connect()
            except Exception as e:
                self.view_controller.print_error("Connection could not be opened.")
                print e
                
    def change_speed(self, speed):
        '''
        Calls local __change_speed method
        
        @param speed: new speed
        '''
        try:
            self.__change_speed(speed)
        except Exception as e:
            self.view_controller.print_error("Speed could not be changed.")
            print e
        
    def __connect(self):
        '''
        Connect serial port
        
        @raise Exception: raises an exception if a connection already exists
        '''
        if not self.is_connected():
            self.view_controller.print_info("Opening connection...")
            self.view_controller.change_connection_state(True)
            
            try:
                self.serial_controller.connect()
            except Exception as e:
                self.view_controller.change_connection_state(self.connected)
                raise e
            
            self.connected = True
            self.view_controller.print_notify("Connection opened.")
        else:
            raise Exception("Connection is opened.")
        
    def __disconnect(self):
        '''
        Disconnect serial port
        
        @raise Exception: raises an exception if a connection does not exist
        '''
        if self.is_connected():
            self.view_controller.print_info("Closing connection...")
            self.view_controller.change_connection_state(False)
            
            try:
                self.serial_controller.disconnect()
            except Exception as e:
                self.view_controller.change_connection_state(self.connected)
                raise e
            
            self.connected = False
            self.view_controller.print_notify("Connection closed.")
        else:
            raise Exception("Connection is not opened.")
        
    def __change_speed(self, speed):
        '''
        Changes motor speed
        
        @param speed: new speed
        @raise Exception: raises an exception if a connection does not exist
        '''
        if self.is_connected():
            self.view_controller.print_info("Changing motor speed to " + str(speed))
            self.serial_controller.change_speed(speed)
            self.view_controller.print_notify("Speed changed to " + self.serial_controller.readln())
        else:
            raise Exception("Connection is not opened.")